#include "sfsis.h"
#include "debug.h"
#include "helper.h"
static SF_Boolen isOnline = SF_False;
static SF_Boolen isHostAlive = SF_False;
//初始化完成后，在启动时仅发生一次的事件
void sf_onstart_action(void) //启动时执行的事项
{
    gCfgCommonPtr->brightness = gCfgCommonPtr->startBrightness; //设备启动亮度恢复默认值
    gCfgCommonPtr->onOff = gCfgCommonPtr->startOnOff;           //开启后保证设备的开关机状态
}

//收到命令且成功处理时，触发的事件
void sf_cmd_action(void)
{
    DPRINT(DEBUG_INFO, "[SFMAIN] Cmd action.\r\n");
}

//未配对时，触发一次该事件
void sf_notpair_action(void)
{
    DPRINT(DEBUG_INFO, "[SFMAIN] Not pair action.\r\n");
    mwl_dev_setLed(MWL_LED_GREEN, MWL_DEV_LED_SHORT_BLINK);
    gCfgFirmwarePtr->led1State = MWL_DEV_LED_SHORT_BLINK;
}

//连线成功时，触发的事件
void sf_online_edge_action(void)
{
    mwl_dev_setLed(MWL_LED_GREEN, MWL_DEV_LED_ON);
    gCfgFirmwarePtr->led1State = MWL_DEV_LED_ON;
    DPRINT(DEBUG_INFO, "[SFMAIN] Wireless connected.\r\n");
    isOnline = SF_True;
}

//断线时，触发的事件
void sf_offline_edge_action(void)
{
    mwl_dev_setLed(MWL_LED_GREEN, MWL_DEV_LED_LONG_BLINK);
    gCfgFirmwarePtr->led1State = MWL_DEV_LED_LONG_BLINK;
    DPRINT(DEBUG_INFO, "[SFMAIN] Wireless disconnected.\r\n");
    isOnline = SF_False;
}

void sf_on_host_alive_action(void)
{
    isHostAlive = SF_True;
}
void sf_on_host_dead_action(void)
{
    isHostAlive = SF_False;
}
void sf_host_alive_task(void)
{
}
void sf_host_dead_task(void)
{
}

//在线持续任务
void sf_online_task(void)
{
}

//离线持续任务
void sf_offline_task(void)
{
}

//通用状态任务
void sf_common_task(void)
{
}

void key_process(void)
{
    if (gCfgFirmwarePtr->adcVal < 90 && gCfgFirmwarePtr->adcVal > 60)
    {
        gCfgCommonPtr->brightness -= 10;
        if (gCfgCommonPtr->brightness < 100)
        {
            gCfgCommonPtr->brightness = 100;
        }
    }
    else if (gCfgFirmwarePtr->adcVal < 210 && gCfgFirmwarePtr->adcVal > 170)
    {
        //rise brightness
        if (gCfgCommonPtr->brightness < 245)
        {
            gCfgCommonPtr->brightness += 10;
        }
        else
        {
            gCfgCommonPtr->brightness = 255;
        }
    }
}

void sf_key_on_press_action(void)
{
    if (gCfgFirmwarePtr->adcVal <= 20)
    {
        if (gCfgCommonPtr->onOff)
        {
            gCfgCommonPtr->onOff = 0;
        }
        else
        {
            gCfgCommonPtr->onOff = 1;
        }
    }
    if (isHostAlive == SF_True)
    {
        //do nothing
    }
    else
    {
        if (gCfgFirmwarePtr->adcVal < 160 && gCfgFirmwarePtr->adcVal > 120)
        {
            Color color;
            static su8 coloroffset = 0;
            switch (gCfgModePtr->mode)
            {
            case CONFIG_MODE_MONOCHROME:
                if (coloroffset < 250)
                {
                    coloroffset += 25;
                    helper_wheel(coloroffset, &color);
                    gCfgModePtr->context.monochrome.color.R = color.R;
                    gCfgModePtr->context.monochrome.color.G = color.G;
                    gCfgModePtr->context.monochrome.color.R = color.B;
                }
                else
                {
                    coloroffset = 0;
                    gCfgModePtr->mode = CONFIG_MODE_RAINBOW;
                    gCfgModePtr->context.rainbow.speed = 110;
                }

                break;
            case CONFIG_MODE_RAINBOW:
                gCfgModePtr->mode = CONFIG_MODE_SHADING;
                gCfgModePtr->context.shading.speed = 255;
                break;
            case CONFIG_MODE_SHADING:
                coloroffset = 0;
                helper_wheel(coloroffset, &color);
                gCfgModePtr->context.monochrome.color.R = color.R;
                gCfgModePtr->context.monochrome.color.G = color.G;
                gCfgModePtr->context.monochrome.color.R = color.B;
                gCfgModePtr->mode = CONFIG_MODE_MONOCHROME;
                break;
            default:
                coloroffset += 25;
                helper_wheel(coloroffset, &color);
                gCfgModePtr->context.monochrome.color.R = color.R;
                gCfgModePtr->context.monochrome.color.G = color.G;
                gCfgModePtr->context.monochrome.color.R = color.B;
                gCfgModePtr->mode = CONFIG_MODE_MONOCHROME;
                break;
            }
        }
        key_process();
    }
}

void sf_key_on_release_action(void)
{
}

void sf_key_on_long_press_task(void)
{
    if (isHostAlive == SF_False)
    {
        key_process();
    }
}